/**
 * 
 */
package vn.edu.hungvuongaptech.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import vn.edu.hungvuongaptech.common.Constant;
import vn.edu.hungvuongaptech.model.WorkModel;
import vn.edu.hungvuongaptech.util.DataUtil;

/**
 * @author PCHOME
 *
 */
public class WorkDAO {
	public static ArrayList<WorkModel> getAllWorks(){
		ArrayList<WorkModel> workModelList = new ArrayList<WorkModel>();
		try {
			String sql = Constant.SQL_RES.getString("timesheet.sql.getAllWorks");
			ResultSet rs = DataUtil.executeQuery(sql);
			WorkModel workModel = null;
			while (rs.next()) {
				workModel = new WorkModel();
				workModel.setWorkId(rs.getInt("WorkId"));
				workModel.setWorkName(rs.getString("WorkName"));
				workModel.setWorkDescription(rs.getString("WorkDescription"));
				workModelList.add(workModel);
			}
			return workModelList;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static WorkModel getWorkByID(int workID){
		WorkModel workModel = new WorkModel();		
		try {
			PreparedStatement preparedStatement = DataUtil
					.getConnection()
					.prepareStatement(
							Constant.SQL_RES
									.getString("timesheet.sql.getWorkByID"));
			preparedStatement.setLong(1, workID);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				workModel.setWorkId(rs.getInt("WorkID"));
				workModel.setWorkName(rs.getString("WorkName"));
				workModel.setWorkDescription(rs.getString("WorkDescription"));
			}
			return workModel;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
